package com.wjs.gulimall.gulimallproduct.service.impl;

import com.wjs.gulimall.gulimallproduct.VO.AttrGroupWithAttrVo;
import com.wjs.gulimall.gulimallproduct.VO.SpuItemAttrGroupVo;
import com.wjs.gulimall.gulimallproduct.entity.AttrEntity;
import com.wjs.gulimall.gulimallproduct.service.AttrService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.wjs.gulimall.gulimallproduct.dao.AttrGroupDao;
import com.wjs.gulimall.gulimallproduct.entity.AttrGroupEntity;
import com.wjs.gulimall.gulimallproduct.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //如果id为0
        String key =(String) params.get("key");
        //封装查询条件
        QueryWrapper<AttrGroupEntity> wrapper=new QueryWrapper<>();
        //如果参数传入了KEY
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        if(catelogId==0){
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
            //如果不为0
        }else {
            wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 根据分类id查出所有分组及其对应属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrVo> getGroupWithAttr(Long catelogId) {
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrVo> data = groupEntities.stream().map((obj) -> {
            AttrGroupWithAttrVo vo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(obj, vo);
            //更具分组id查出其下所有属性
            List<AttrEntity> attrEntities = attrService.getRelationList(obj.getAttrGroupId());
            vo.setAttrs(attrEntities);
            return vo;
        }).collect(Collectors.toList());
        return data;
    }

    @Override
    public List<SpuItemAttrGroupVo> getgroupwithAttrsBySpuId(Long spuId, Long catalogId) {
        List<SpuItemAttrGroupVo> attrGroupVos=this.baseMapper.getgroupwithAttrsBySpuId(spuId,catalogId);
        return attrGroupVos;
    }

}